﻿// -------------------------------------------------------------------------------------------------------------------------------
// <copyright file="TestAddIndexedCMFlattenDenseMatrix.cs" company="Exul">
//     Copyright (c) Exul. All rights reserved.
// </copyright>
// <author>
//     Exul
// </author>
// -------------------------------------------------------------------------------------------------------------------------------

namespace ExulLibrary.Mathematics.LinearAlgebra.Sequential.Tests.CMFlattenDenseMatrixTest.DoubleExtensions
{
    #region Usings

    using System;
    using System.Globalization;

    using ExulLibrary.Exceptions;
    using ExulLibrary.Mathematics.LinearAlgebra.Sequential.DoubleExtensions;

    using NUnit.Framework;

    using Extensions = ExulLibrary.Mathematics.LinearAlgebra.Sequential.DoubleExtensions.CMFlattenDenseMatrixExtensions;

    #endregion

    /// <summary>
    ///     Test suite for <see cref="Extensions.Add(CMFlattenDenseMatrix{double},int[],int[],CMFlattenDenseMatrix{double})"/>.
    /// </summary>
    [TestFixture]
    [Category("ExulLibrary.Mathematics.LinearAlgebra.Sequential.DoubleExtensions.CMFlattenDenseMatrixExtensions")]
    public sealed class TestAddIndexedCMFlattenDenseMatrix
    {
        /// <summary>
        ///     Tests the method when indexes of augend elements are non-negative and less than
        ///     size of dimensions of augend matrix.
        /// </summary>
        [Test]
        public void WhenAugendIndexesAreValid()
        {
            // Arrange
            var augendData = new double[]
                             {
                                 1, 2, 3,
                                 4, 5, 6,
                                 7, 8, 9
                             };
            var augend = new CMFlattenDenseMatrix<double>(3, 3, augendData);

            var augendRowsIndexes = new[] { 1, 2 };
            var augendColumnsIndexes = new[] { 0, 2 };
            var addendData = new double[]
                             {
                                 10, 20,
                                 30, 40
                             };
            var addend = new CMFlattenDenseMatrix<double>(2, 2, addendData);
            var expectedResult = new double[]
                                 {
                                     1, 12, 23,
                                     4, 5, 6,
                                     7, 38, 49
                                 };

            // Act
            augend.Add(augendRowsIndexes, augendColumnsIndexes, addend);

            // Assert
            Assert.That(augend.Values, Is.EquivalentTo(expectedResult));
        }

        /// <summary>
        ///     Tests the method when augend rows indexes collection is empty.
        /// </summary>
        [Test]
        public void WhenRowsIndexesCollectionIsEmpty()
        {
            // Arrange
            var augendData = new double[]
                             {
                                 1, 2, 3,
                                 4, 5, 6,
                                 7, 8, 9
                             };
            var augend = new CMFlattenDenseMatrix<double>(3, 3, augendData);

            var augendRowsIndexes = new int[0];
            var augendColumnsIndexes = new[] { 0, 2 };
            var addendData = new double[]
                             {
                                 10, 20,
                                 30, 40
                             };
            var addend = new CMFlattenDenseMatrix<double>(2, 2, addendData);
            var expectedResult = new double[]
                                 {
                                     1, 2, 3,
                                     4, 5, 6,
                                     7, 8, 9
                                 };

            // Act
            augend.Add(augendRowsIndexes, augendColumnsIndexes, addend);

            // Assert
            Assert.That(augend.Values, Is.EquivalentTo(expectedResult));
        }

        /// <summary>
        ///     Tests the method when augend columns indexes collection is empty.
        /// </summary>
        [Test]
        public void WhenColumnsIndexesCollectionIsEmpty()
        {
            // Arrange
            var augendData = new double[]
                             {
                                 1, 2, 3,
                                 4, 5, 6,
                                 7, 8, 9
                             };
            var augend = new CMFlattenDenseMatrix<double>(3, 3, augendData);

            var augendRowsIndexes = new[] { 1, 2 };
            var augendColumnsIndexes = new int[0];
            var addendData = new double[]
                             {
                                 10, 20,
                                 30, 40
                             };
            var addend = new CMFlattenDenseMatrix<double>(2, 2, addendData);
            var expectedResult = new double[]
                                 {
                                     1, 2, 3,
                                     4, 5, 6,
                                     7, 8, 9
                                 };

            // Act
            augend.Add(augendRowsIndexes, augendColumnsIndexes, addend);

            // Assert
            Assert.That(augend.Values, Is.EquivalentTo(expectedResult));
        }

#if SAFE

        /// <summary>
        ///     Tests the method when augend is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenAugendIsNull()
        {
            // Arrange
            var augendRowsIndexes = new[] { 1, 2 };
            var augendColumnsIndexes = new[] { 0, 2 };
            var addendData = new double[]
                             {
                                 10, 20,
                                 30, 40
                             };
            var addend = new CMFlattenDenseMatrix<double>(2, 2, addendData);

            // Act and Assert
            Assert.That(
                () => CMFlattenDenseMatrixExtensions.Add(null, augendRowsIndexes, augendColumnsIndexes, addend),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("augend"));
        }

        /// <summary>
        ///     Tests the method when addend is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenAddendIsNull()
        {
            // Arrange
            var augendData = new double[]
                             {
                                 1, 2, 3,
                                 4, 5, 6,
                                 7, 8, 9
                             };
            var augend = new CMFlattenDenseMatrix<double>(3, 3, augendData);

            var augendRowsIndexes = new[] { 1, 2 };
            var augendColumnsIndexes = new[] { 0, 2 };

            // Act and Assert
            Assert.That(
                () => augend.Add(augendRowsIndexes, augendColumnsIndexes, null),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("addend"));
        }

        /// <summary>
        ///     Tests the method when augend rows indexes collection is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenAugendRowsIndexesCollectionIsNull()
        {
            // Arrange
            var augendData = new double[]
                             {
                                 1, 2, 3,
                                 4, 5, 6,
                                 7, 8, 9
                             };
            var augend = new CMFlattenDenseMatrix<double>(3, 3, augendData);

            var augendColumnsIndexes = new[] { 0, 2 };
            var addendData = new double[]
                             {
                                 10, 20,
                                 30, 40
                             };
            var addend = new CMFlattenDenseMatrix<double>(2, 2, addendData);

            // Act and Assert
            Assert.That(
                () => augend.Add(null, augendColumnsIndexes, addend),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("augendRowsIndexes"));
        }

        /// <summary>
        ///     Tests the method when augend columns indexes collection is <see langword="null"/>.
        /// </summary>
        [Test]
        public void WhenAugendColumnsIndexesCollectionIsNull()
        {
            // Arrange
            var augendData = new double[]
                             {
                                 1, 2, 3,
                                 4, 5, 6,
                                 7, 8, 9
                             };
            var augend = new CMFlattenDenseMatrix<double>(3, 3, augendData);

            var augendRowsIndexes = new[] { 1, 2 };
            var addendData = new double[]
                             {
                                 10, 20,
                                 30, 40
                             };
            var addend = new CMFlattenDenseMatrix<double>(2, 2, addendData);

            // Act and Assert
            Assert.That(
                () => augend.Add(augendRowsIndexes, null, addend),
                Throws.TypeOf<ArgumentNullException>().With.Property("ParamName").EqualTo("augendColumnsIndexes"));
        }

        /// <summary>
        ///     Tests the method when an augend row index is negative or greater than or equal to rows count of augend matrix.
        /// </summary>
        /// <param name="rowIndex">
        ///     The row index.
        /// </param>
        /// <param name="location">
        ///     The location of row index.
        /// </param>
        [Test]
        public void WhenAnAugendRowIndexIsOutOfRange([Values(-1, 3)] int rowIndex, [Values(0, 1)] int location)
        {
            // Arrange
            var augendData = new double[]
                             {
                                 1, 2, 3,
                                 4, 5, 6,
                                 7, 8, 9
                             };
            var augend = new CMFlattenDenseMatrix<double>(3, 3, augendData);

            var augendRowsIndexes = new int[2];
            augendRowsIndexes[location] = rowIndex;

            var augendColumnsIndexes = new[] { 0, 2 };
            var addendData = new double[]
                             {
                                 10, 20,
                                 30, 40
                             };
            var addend = new CMFlattenDenseMatrix<double>(2, 2, addendData);

            // Act and Assert
            var argumentName = string.Format(CultureInfo.InvariantCulture, "augendRowsIndexes[{0}]", location);
            Assert.That(
                () => augend.Add(augendRowsIndexes, augendColumnsIndexes, addend),
                Throws.TypeOf<ElementIndexIsOutOfRangeException>().With.Property("ParamName").EqualTo(argumentName));
        }

        /// <summary>
        ///     Tests the method when an augend column index is negative or greater than or equal to
        ///     columns count of augend matrix.
        /// </summary>
        /// <param name="columnIndex">
        ///     The column index.
        /// </param>
        /// <param name="location">
        ///     The location of column index.
        /// </param>
        [Test]
        public void WhenAnAugendColumnIndexIsOutOfRange([Values(-1, 3)] int columnIndex, [Values(0, 1)] int location)
        {
            // Arrange
            var augendData = new double[]
                             {
                                 1, 2, 3,
                                 4, 5, 6,
                                 7, 8, 9
                             };
            var augend = new CMFlattenDenseMatrix<double>(3, 3, augendData);

            var augendRowsIndexes = new[] { 1, 2 };
            var augendColumnsIndexes = new int[2];
            augendColumnsIndexes[location] = columnIndex;

            var addendData = new double[]
                             {
                                 10, 20,
                                 30, 40
                             };
            var addend = new CMFlattenDenseMatrix<double>(2, 2, addendData);

            // Act and Assert
            var argumentName = string.Format(CultureInfo.InvariantCulture, "augendColumnsIndexes[{0}]", location);
            Assert.That(
                () => augend.Add(augendRowsIndexes, augendColumnsIndexes, addend),
                Throws.TypeOf<ElementIndexIsOutOfRangeException>().With.Property("ParamName").EqualTo(argumentName));
        }

#endif
    }
}